﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;


namespace SettingModule.Views.FieldSettingView
{
    public class FieldSettingPresentationModel:IFieldSettingPresentationModel
    {
        private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public FieldSettingPresentationModel(IFieldSettingView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManage, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManage;
            this.moduleManager = moduleManager;
            this.View = view;
            view.SetModel(this);
            InitialDataSource();
            SelectedGroupId = -1;
        }

        public void InitialDataSource()
        {
            _domaincontext = new ELMServiceClient();
            _domaincontext.GetAllFieldAsync();
            _domaincontext.GetAllFieldCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        DataSource = data.Result;
                    }

                };
            _domaincontext.GetAllFieldGroupAsync();
            _domaincontext.GetAllFieldGroupCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        GroupDataSource = data.Result;
                    }
                };
            _domaincontext.GetAllFieldTypeAsync();
            _domaincontext.GetAllFieldTypeCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    TypesDataSource = data.Result;
                }
            };
        }

       
        
        
        
        #region IFieldSettingPresentationModel Members

        public IFieldSettingView View
        {
            get;
            set;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion



        #region IFieldSettingPresentationModel Members

        private ObservableCollection<ELMField> _dataSource;
        public ObservableCollection<ELMField> DataSource
        {
            get
            { return _dataSource; }
            set
            {
                _dataSource = value;
                NotifyPropertyChanged("DataSource");
            }
        }

        #endregion

        #region IFieldSettingPresentationModel Members


        public void AddField(string name,int groupid,string typeid)
        {
            _domaincontext = new ELMServiceClient();
            _domaincontext.CheckFieldNameIsExstAsync(name);
            _domaincontext.CheckFieldNameIsExstCompleted += (sender1, data1) =>
            {
                if (data1.Error == null && data1.Result == false)
                {
                    _domaincontext.AddFieldAsync(name,groupid,typeid);
                    
                    _domaincontext.AddFieldCompleted += (sender2, data2) =>
                    {
                        if (data2.Error == null)
                        {
                            SelectedGroup();
                            MessageBox.Show("字段添加成功！");
                            
                        }
                    };
                    
                }
                else
                {
                    MessageBox.Show("字段名称已存在！");

                }
            };
        }

        #endregion

        #region IFieldSettingPresentationModel Members


        public void Delete(Guid Id)
        {
            _domaincontext = new ELMServiceClient();
            _domaincontext.CheckFieldIsInContentTypeAsync(DataSource.Single(c => c.FieldId == Id));
            _domaincontext.CheckFieldIsInContentTypeCompleted+=(sender2,e2)=>
                {
                    if (e2.Error == null)
                    {
                        if (!e2.Result)
                        {
                            _domaincontext.SetFieldStatusAsync(Id, true);
                            _domaincontext.SetFieldStatusCompleted += (sender, e) =>
                            {
                                if (e.Error == null)
                                {
                                    SelectedGroup();

                                }
                            };
                            MessageBox.Show("操作完成！");
                        }
                        else
                        {
                            MessageBox.Show("该字段已经被使用中，无法删除！");
                        }
                    }
                };
           
        }

        #endregion

        #region IFieldSettingPresentationModel Members

        private ObservableCollection<t_FieldGroup> _groupDataSource;
        public ObservableCollection<t_FieldGroup> GroupDataSource
        {
            get
            {
               return _groupDataSource;
            }
            set
            {
                _groupDataSource=value;
                NotifyPropertyChanged("GroupDataSource");
            }
        }

        public void SelectedGroup()
        {
              _domaincontext = new ELMServiceClient();
              if (SelectedGroupId == -1)
            {
              
            _domaincontext.GetAllFieldAsync();
            _domaincontext.GetAllFieldCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        DataSource = data.Result;
                    }

                };
            }
            else
            {
                _domaincontext.GetFieldsByGroupIdAsync(SelectedGroupId);
            _domaincontext.GetFieldsByGroupIdCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        DataSource = data.Result;
                    }

                };
            }
        }

        #endregion

        #region IFieldSettingPresentationModel Members

        private ObservableCollection<t_FieldType> _typesDataSource;
        public ObservableCollection<t_FieldType> TypesDataSource
        {
            get
            {
                return _typesDataSource;
            }
            set
            {
                _typesDataSource = value;
                NotifyPropertyChanged("TypesDataSource");
            }
        }

        #endregion

        #region IFieldSettingPresentationModel Members


     

        public int SelectedGroupId
        {
            get;
            set;
        }

        #endregion
    }
}
